package com.doit.day01;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.HFileOutputFormat2;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputFormat;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
//spark
public class CreateHFileDemo {

    public static class HFileMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue>{
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue>.Context context) throws IOException, InterruptedException {
            String line = value.toString();
            //针对这一行数据进行切割处理
            String[] arr = line.split(",");
            //写出去
            /**
             * keyvalue:rowkey  liezu  lie value
             * 每次set是set的一列的值
             */
            context.write(new ImmutableBytesWritable(arr[0].getBytes()),new KeyValue(arr[0].getBytes(),"f1".getBytes(),"age".getBytes(),arr[2].getBytes()));
            context.write(new ImmutableBytesWritable(arr[0].getBytes()),new KeyValue(arr[0].getBytes(),"f1".getBytes(),"name".getBytes(),arr[1].getBytes()));
            context.write(new ImmutableBytesWritable(arr[0].getBytes()),new KeyValue(arr[0].getBytes(),"f2".getBytes(),"gender".getBytes(),arr[3].getBytes()));
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        System.setProperty("HADOOP_USER_NAME","root");

        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","linux01:2181,linux02:2181,linux03:2181");
        Job job = Job.getInstance(conf);

        //设置mapper类
        job.setMapperClass(HFileMapper.class);

        //map的输出类型
        job.setMapOutputKeyClass(ImmutableBytesWritable.class);
        job.setMapOutputValueClass(KeyValue.class);

        //设置reducetask的个数
        job.setNumReduceTasks(0);

        //输出成hfile，那你还需要告诉我具体给哪一个表输出
        //创建一个连接对象
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tableName = TableName.valueOf("test:user_info1");
//        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
//        TableDescriptor build = tableDescriptorBuilder.build();
        Table table = conn.getTable(tableName);
        //就是hbase中具体的region的值
        RegionLocator regionLocator = conn.getRegionLocator(tableName);
        HFileOutputFormat2.configureIncrementalLoad(job,table,regionLocator);

        //设置输出的类型
        job.setOutputFormatClass(HFileOutputFormat2.class);

        //输入输出路径
        FileInputFormat.setInputPaths(job,new Path("data/test.txt"));
        FileOutputFormat.setOutputPath(job,new Path("hdfs://linux01:8020/output/"));

        //开启程序
        job.waitForCompletion(true);


    }

}
